package com.woniu.gf.pawn.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.gf.pawn.comm.core.domain.Product;
import com.woniu.gf.pawn.comm.core.domain.ProductProperty;
import com.woniu.gf.pawn.comm.core.domain.ProductPropertyValue;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.product.parma.AddProductProperParam;
import com.woniu.gf.pawn.product.parma.FindProductBigTypePageParam;
import com.woniu.gf.pawn.product.parma.ListIdParam;
import com.woniu.gf.pawn.product.parma.ShowProductPropertyParam;
import com.woniu.gf.pawn.service.IProductPropertyService;
import com.woniu.gf.pawn.utils.BindingResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/product-property")
public class ProductPropertyController {

    @Autowired
    private IProductPropertyService productPropertyService;

    /**
     * 新增属性和属性值
     * @param addProductProperParam
     * @param result
     * @return
     */
    @ApiOperation("新增属性和属性值")
    @PostMapping("addProductProperty")
    public ResultDto addProductProperty(@RequestBody @Valid AddProductProperParam addProductProperParam, BindingResult result){
        System.out.println();
        if(addProductProperParam.getValues().size()==0) return ResultDto.fail(202,"新增失败","数组参数为空");
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()){
            ProductProperty productProperty = new ProductProperty();
            List<ProductPropertyValue> productPropertyValues=new ArrayList<>();
            BeanUtils.copyProperties(addProductProperParam,productProperty);
            for (int i=0;i<addProductProperParam.getValues().size();i++){
                ProductPropertyValue productPropertyValue = new ProductPropertyValue();
                productPropertyValue.setProductPropertyValueName(addProductProperParam.getValues().get(i));
                productPropertyValues.add(productPropertyValue);
            }
            productPropertyService.addProductProperty(productProperty,productPropertyValues);
            return ResultDto.success(200,"新增属性和对应的属性值成功");
        }
        return ResultDto.fail(202,"新增失败",bindingResult);
    }
    /**
     * 查询分页
     */
    @ApiOperation("属性和属性值展示")
    @GetMapping("findPage")
    public ResultDto findPage(@Valid FindProductBigTypePageParam findProductBigTypePageParam, BindingResult result) {
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            IPage<ShowProductPropertyParam> productBigTypeIPage = productPropertyService.selectPageVo(findProductBigTypePageParam);

            return ResultDto.success(200, "查询成功",productBigTypeIPage );
        }
        return ResultDto.fail(202, "查询失败",bindingResult );
    }
    /**
     * 删除属性
     */
    @ApiOperation("删除属性")
    @PostMapping("deleteProductProperty")
    public  ResultDto  deleteProductProperty(@RequestBody ListIdParam listIdParam)  {

        if(listIdParam.getIds()==null)  return  ResultDto.fail(202,"删除失败","参数为空");
        productPropertyService.deleteProductProperty(listIdParam.getIds());
        return  ResultDto.success(200,"删除成功");
    }
}

